package org.isource.admin;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.isource.common.PMF;
import org.isource.pojo.ThirdParty;

@SuppressWarnings("serial")
public class GetThirdParty extends HttpServlet {

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html");
		String key = req.getParameter("key");
		if (key == null) {
			resp.getWriter().println("No key provided.");
			return;
		}
		ThirdParty tp;

		tp = findJDO(Long.valueOf(key));

		resp.getWriter().println(
				"<form action=\"updateThirdParty\" method=\"post\">");
		resp.getWriter().println(
				"<input name=\"key\" type=\"hidden\" value=\"" + key + "\"/>");
		resp.getWriter().println("<table>");
		resp.getWriter().println("<tr>");
		resp.getWriter().println(
				"<th>name</th><td><input name=\"name\" type=\"text\" value=\""
						+ tp.getName() + "\"/></td>");
		resp.getWriter().println(
				"<th>version</th><td><input name=\"version\" type=\"text\" value=\""
						+ tp.getVersion() + "\"/></td>");
		resp.getWriter().println(
				"<th>category</th><td><input name=\"category\" type=\"text\" value=\""
						+ tp.getCategory() + "\"/></td>");
		resp.getWriter().println("</tr>");
		resp
				.getWriter()
				.println(
						"<tr><td><input type=\"submit\" value=\"Update ThirdParty\"></td></tr>");
		resp.getWriter().println("</table>");
		resp.getWriter().println("</form>");
		resp.getWriter().println(
				"<form action=\"deleteThirdParty\" method=\"post\">");
		resp.getWriter().println(
				"<input name=\"key\" type=\"hidden\" value=\"" + key + "\"/>");
		resp
				.getWriter()
				.println(
						"<input type=\"submit\" value=\"Delete ThirdParty\"></td></tr>");
		resp.getWriter().println("</form>");
	}

	private ThirdParty findJDO(long key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			return pm.getObjectById(ThirdParty.class, key);
		} finally {
			pm.close();
		}
	}
}
